class NarrowStructure {
	private NarrowElement head;
	private long size;

	NarrowStructure(long size) {
		this.size = size;
		NarrowElement node = null;
		
		for(int i = 0; i < this.size; i++) {
			node = new NarrowElement(node);
		}
			
		this.head = node;
	}
	
	long getSize() {
		return this.size;
	}
	
	long traverse() {
		long visited = 0;
		NarrowElement node = this.head;
		
		while(node != null) {
			node = node.getNext();
			visited++;
		}
		
		return visited;
	}
	
	long traverseWithReplace(int replaceRate) {
		long totalVisited = 0;
		long visited = 0;
		long replaced = 0;
		NarrowElement node = this.head;
		NarrowElement nextNode;
		
		while(node != null) {
			nextNode = node.getNext();
			visited++;
			if(visited == replaceRate && nextNode != null) {
				//System.out.println("traverseWithReplace - replacing after "+visited+" nodes");
				nextNode = new NarrowElement(nextNode.getNext());
				node.setNext(nextNode);
				replaced++;
				totalVisited += visited;
				visited = 0;
			}
			node = nextNode;
		}
		totalVisited += visited;
		//System.out.println("traverseWithReplace - visited "+totalVisited+" nodes, replaced "+replaced+" nodes");
		return totalVisited;
	}
}
